var mongoose = require('mongoose');
var fs = require('fs');

var schemas = require('../models/models.js')

//creates a new user - queries 'name' for username and 'pw' for password are required!
exports.createUser = function(req, res){
	if(!(req.query.name || req.query.pw)) {res.send('name and pw not provided in your request'); return}
	var newUser = mongoose.model('User', schemas.UserSchema);
	newUser.find({type: "user"}, function(err, found){
		if(err) {console.log(err); res.send('Error in createUser - find'); return}
		for(i in found) {
			if(req.query.name === found[i].name) { res.send('User already exists in the database!'); return }
		}
		var data = new newUser({id: found.length, name: req.query.name, password: req.query.pw, type: "user"})
		data.save(function(err){ if(err) {console.log(err); res.send('Error in createUser - save'); return } res.send('User created successfully'); })		
	})
}

// to get all the users in the database
exports.userGetAll = function(req, res) {
	var newUser = mongoose.model('User', schemas.UserSchema);
	newUser.find({type: "user"}, function(err, found) {
		if(err) {console.log(err); res.send('Error in userGetAll - find'); return;}
		res.send(found);
	})
}

// to get all the users' names
exports.userNamesGetAll = function(req, res){
	var newUser = mongoose.model('User', schemas.UserSchema);
	var names = []
	newUser.find({type : 'user'}, function(err, found){
		if(err){ console.log(err); res.send('Error in userNamesGetAll - find'); return }
			for(i in found){
				names.push(found[i].name)
			}
			//console.log(names)
			res.send({usernames  : names})
	})	
}


// to delete all the users in the database
exports.userDeleteAll = function(req, res) {
	var newUser = mongoose.model('User', schemas.UserSchema);
	newUser.find({type: "user"}, function(err, found) {
		if(err){console.log(err); res.send('Error in userDeleteAll - find'); return}
		for(i in found) {
			found[i].remove()
		}
		res.send('All users deleted successfully')
	})	
}

// to delete a user from the database - N.B: queries 'name' and 'pw' are required!
exports.userDelete = function(req, res){
	if(!(req.query.name || req.query.pw)) {res.send('name and pw not provided in your request'); return}
	var newUser = mongoose.model('User', schemas.UserSchema);
	newUser.find({name: req.query.name}, function(err, found){
		if(err){console.log(err); res.send('Error in userDelete - find'); return}
		for(i in found) {
			if(found[i].password === req.query.pw) { found[i].remove() }
		}
		res.send('User deleted successfully')
	})
}

// to get a single user from the database - N.B: query 'name' and 'pw' required!. This also sends a unique ID for the following authorization
exports.userGet = function(req, res){
	if(!req.query.name) {res.send('name not provided in your request'); return}
	var newUser = mongoose.model('User', schemas.UserSchema);
	newUser.find({name: req.query.name}, function(err, found) {
		if(err){console.log(err); res.send('Error in userGet - find'); return}
		if(found[0] != undefined && req.query.pw === found[0].password){
			var uniqueID=Math.floor(Math.random()*10000);
			while(validIDs.indexOf(uniqueID) != -1){
				uniqueID = Math.floor(Math.random()*10000)
			}
			validIDs.push(uniqueID);
			//console.log('validIDs now: ' + validIDs);
			res.send({
				user: found[0],
				id : uniqueID
			})
		} else { res.send({ user: undefined, id: undefined }) }
	})
}



